// votePage/pages/detail/detail.js
const app = getApp();
import { wxLogin } from '../../../utils/utils'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    autoplay: true,
    interval: 6000,
    duration: 500,
    swiperIdx:0,
    scrollTop: 0,
    indicatorDots: true,
    bannerList:[],
    loading:false,
    data_list:{}
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var _this = this;
    if (options.id) {
      this.setData({
        id: options.id
      })
    }

    wxLogin();
    setTimeout(() => {
      _this.getDetail();
    },10)
  },

  voteTo:function() {
    var _this = this;
    var data_list = this.data.data_list;
    var id = data_list.id;
    if(data_list.today_already_vote < 3) {
      wx.request({
        url: app.globalData.baseNew + 'api/activity/vote/' + id,
        header: {
          Authorization: "Bearer " + wx.getStorageSync('c-token')
        },
        method:'post',
        data: {},
        success(res) {
          wx.hideLoading()
          if (res.statusCode == 200) {
            wx.showToast({
              title: '投票成功',
              icon: 'none',
              duration: 2000
            })
            data_list.today_already_vote = data_list.today_already_vote  + 1;
            data_list.total_vote = data_list.total_vote  + 1;
            _this.setData({
              data_list:data_list
            })
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
  }, 

  onShareAppMessage: function () {
    var { id } = this.data;
    return {
      title: '书香长江投票活动',
      path: '/votePage/pages/detail/detail?id=' + id,
    }
  },
 
  // onShareTimeline: function () {
  //   return {
  //     title: '书香长江投票活动',
  //     path: '/votePage/list/list'
  //   }
  // },

  getDetail:function(){
    console.log(123)
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      loading:false
    })
    var _this = this;
    var { id } = this.data;
    var _this = this;
    wx.request({
      url: app.globalData.baseNew + 'api/activity/info/' + id,
      data: {},
      header: {
        Authorization: "Bearer " + wx.getStorageSync('c-token'),
      },
      success(res) {
        wx.hideLoading()
        // console.log(res)
        if (res.statusCode == 200) {
          console.log(res.data.data)
          // res.data.data
          _this.setData({
            data_list:res.data.data
          })
          _this.setData({
            bannerList:res.data.data.file_list
          })
          
          if(res.data.data.title) {
            wx.setNavigationBarTitle({
              title: '作品详情'  // 设置为你想要的标题
            })
          } else {
            wx.setNavigationBarTitle({
              title: '公司详情'  // 设置为你想要的标题
            })
          }
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
        // _this.setData({
        //   loading:_this.data.worksList.length===0
        // })
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  }
})